<template>
  <div id="agreement">
    <div class="inner">
      <div class="title">{{ props.agreementTitle }}</div>
      <div class="content">
        <div v-for="(item, index) in props.contentList"
          :class="['items', props.indexList.includes(index) ? 'mt-30' : '']" :key="item">
          {{ item }}
        </div>
      </div>
      <div class="party">
        <div class="party-a">
          <div class="ft">
            <span>甲方：方田教育</span>
          </div>
          <div class="date">
            <span>日期：</span>
            <span> {{ resumeInfo.currentDay }}</span>
          </div>
          <img src="../assets/img/seal.png" alt="" srcset="" />
        </div>
        <div class="party-b">
          <div class="ft">
            <div class="sign">
              <span>乙方：</span>
              <!-- v-show="!signature" -->
              <span class="start-sign" v-show="!signature"></span>
              <img :src="signature" class="sign-img" v-if="signature" alt="" />
            </div>
          </div>
          <div class="date">
            <span>日期：</span>
            <span> {{ resumeInfo.currentDay }}</span>
          </div>
        </div>
      </div>
      <a-button type="primary" class="sub-btn" @click="!signature ? startSignature() : dosub()">
        {{ !signature ? '点击签字' : '确定' }}
      </a-button>
    </div>
  </div>

  <commonModal title="按住鼠标在下方区域签名" ref="myModal">
    <template #default>
      <vueEsign class="esign" ref="esign" :height="502" />
      <div class="btn-area">
        <div class="btns" @click="reset">清除重写</div>
        <div class="btns" @click="comfirm">提交签字</div>
      </div>
    </template>
  </commonModal>
  <ElBacktop v-if="!showSignature" :bottom="100" :right="200" target="#agreement" class="back-top">
    <img src="../assets/img/join/toTop.png" alt="" srcset="" />
  </ElBacktop>
</template>

<script setup lang="ts">
import commonModal from '@/views/index/commonModal.vue';
import { toPng } from 'html-to-image';
import vueEsign from 'vue-esign';
import { ElBacktop } from 'element-plus';
import { message } from 'ant-design-vue';
import { dataURLtoFile } from "../assets/common/index"
import NProgress from 'nprogress';
const esign = ref<any>(null);
const myModal = ref<any>(null);
const showSignature = ref<boolean>(false);
const signature = ref<string>('');
const props = defineProps(['contentList', 'agreementTitle', 'indexList']);
const emits = defineEmits(['doSubmit']);
import { useStore } from '@/store';
const store = useStore();
const { resumeInfo } = store as any;
const startSignature = async () => {
  await nextTick();
  myModal.value.show = true;
};
const reset = () => {
  esign?.value?.reset();
};
const comfirm = async () => {
  try {
    const res = await esign?.value?.generate();
    signature.value = res;
    myModal.value.show = false;
  } catch (e) {
    message.error('签字不能为空');
  }
};

const dosub = async () => {
  // const node = document.querySelector("#agreement") as HTMLDivElement;
  // NProgress.start();
  // const dataUrl = await toPng(node);
  // NProgress.done();
  const dataUrls = signature.value;
  const file = await dataURLtoFile(dataUrls, props.agreementTitle + ".png")
  emits('doSubmit', file);
};
</script>

<style lang="scss" scoped>
#agreement {
  width: 100vw;
  height: calc(100vh - 72px);
  margin-top: 72px;
  overflow: auto;
  box-sizing: border-box;
  color: #333;
  padding-top: 20px;
  box-sizing: border-box;
  overflow: auto;

  .inner {
    padding: 40px;
    box-sizing: border-box;
    width: 1200px;
    background: #fff;
    margin: 0 auto;
    border-radius: 12px;
    position: relative;
    min-height: 85vh;
  }
}

.title {
  width: 100%;
  height: 40px;
  opacity: 1;
  font-size: 18px;
  font-weight: normal;
  line-height: 25px;
  text-align: center;
  letter-spacing: 0em;
  font-variation-settings: 'opsz' auto;
  color: #212121;
}

.content {
  .items {
    margin-top: 5px;
  }
}

.mt-30 {
  margin-top: 30px !important;
}

.btn-area {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;

  .btns {
    line-height: 40px;
    text-align: center;
    color: #fff;
    width: 120px;
    height: 40px;
    background: #209ee4;
    border-radius: 8px;
    cursor: pointer;

    &:nth-child(1) {
      margin-right: 20px;
      color: #f56c56;
      background: #f5f7fb;
      border: 1px solid #b3c0c8;
    }
  }
}

.back {
  position: absolute;
  top: 30px;
  left: 30px;
  display: flex;
  color: #333;
  font-size: 16px;
  display: flex;
  align-items: center;

  span {
    margin-left: 10px;
  }
}

.sign {
  display: flex;
  height: 70px;
  align-items: center;

  .start-sign {
    // color: #409eff;
    width: 173px;
    height: 30px;
    background: #f4f6f8;
    border-radius: 5px;
  }

  .sign-img {
    margin-left: 20px;
    width: 130px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }
}

.party {
  display: flex;
  justify-content: space-between;
}

.party-a {
  // margin: 24px 0;
  margin-top: 24px;
  line-height: 26px;
  position: relative;

  img {
    width: 70px;
    position: absolute;
    left: 55%;
    top: -10%;
  }
}

.date {
  span {
    &:nth-child(2n) {
      text-decoration: underline;
      margin: 0 5px;
    }
  }
}

.sub-btn {
  border-radius: 5px;
  text-align: center;
  color: #fff;
  font-size: 14px;
  margin: 0 auto;
  margin-bottom: 10px;
  margin-top: 20px;
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);

  span {
    font-size: 14px !important;
  }
}

.back-top {
  img {
    width: 40px;
    height: 40px;
  }
}

.esign {
  position: relative;
  z-index: 100;
  background: #f5f7fb !important;
}
</style>
